<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class laporan_laba extends CI_Controller {

	function __construct() {
		parent::__construct();

		//jika digunakan di mayoritas function, panggil disini
		//model
		$this->load->model('m_laporan_laba');

		//library breadcrum/untuk navigasi
		$this->load->library('breadcrumb');		
    //helper
    $this->load->helper('formatrp');
    $this->load->helper('id_date');
	}

	function index() {
		//rule type pada function ini: Read
		$this->rule->type('R');

    //Menghapus session pencarian laba
    $this->session->unset_userdata('laba_tgl_awal');
    $this->session->unset_userdata('laba_tgl_akhir');
    $this->session->unset_userdata('laba_kelompok');

		//paging dan ambil data dari model
		$this->load->library('paging');                         		
		//Load library "paging"
		$config['sql']      				= $this->m_laporan_laba->get();
    //Query SQL - bukan array lho ya, liat model
		$config['per_page'] 				= 15;                            
    //jumlah item tiap halaman
		$data = $this->paging->create_pagination($config);   				
    //Load fungsi di library "paging" untuk membuat pagination. 

		//menghilangkan session jika ada
		$this->session->unset_userdata('keyword');

		//judul halaman
		$judul          = 'Laporan Laba';
		$data['judul']  = $judul;
		
    // Title HTML
		$this->layout->set_title($judul);

    // Memanggil JS
    $this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
    $this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
    //Memanggil CSS
    $this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
    $this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

    //breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Laporan', site_url('laporan_laba#'));
		$this->breadcrumb->add_crumb($judul);

    //Ambil data kelompok
    $data['kelompok'] = $this->m_laporan_laba->get_kelompok();
    //Mendeklarasikan Kode Kelompok
    $data['KdKelompok'] = "";
		//menggunakan theme back
		$this->layout->theme('apotek','laporan_laba/index', $data);
	}

	function cari() {

		//rule type pada function ini: Read
		$this->rule->type('R');

    //helper
    $this->load->helper('id_date');

		//buat cari
		if(isset($_POST)){
			$data['tgl_awal']   = $this->input->post('tgl_awal');
      $data['tgl_akhir']  = $this->input->post('tgl_akhir');
      $data['kelompok']   = $this->input->post('kelompok');
			$this->session->set_userdata('laba_tgl_awal', $data['tgl_awal']);
      $this->session->set_userdata('laba_tgl_akhir', $data['tgl_akhir']);
      $this->session->set_userdata('laba_kelompok', $data['kelompok']);
		} else {
			$data['tgl_awal']   = $this->session->userdata('laba_tgl_awal');
      $data['tgl_akhir']  = $this->session->userdata('laba_tgl_akhir');
      $data['kelompok']   = $this->session->userdata('laba_kelompok');
		}	

		//paging dan ambil data dari model
    if($data['tgl_awal'] != '' && $data['tgl_akhir'] != ''){
  		$this->load->library('paging');                         		//Load the "paging" library
      $config['sql']      				= $this->m_laporan_laba->get_cari(iddate2dbdate($data['tgl_awal']),iddate2dbdate($data['tgl_akhir']),$data['kelompok']);//your SQL - bukan array lho ya...
      $config['per_page'] 				= 15;                            //Display items per page
      $data = $this->paging->create_pagination($config);   				//Load function in "paging" libraryfor create pagination. 
    }else{
      $this->load->library('paging');                             //Load the "paging" library
      $config['sql']              = $this->m_laporan_laba->get_cari_kelompok($data['kelompok']);//your SQL - bukan array lho ya...
      $config['per_page']         = 15;                            //Display items per page
      $data = $this->paging->create_pagination($config);          //Load function in "paging" libraryfor create pagination.
    }
    //Ambil data kelompok
    $data['kelompok']   = $this->m_laporan_laba->get_kelompok();
    //Mendeklarasikan Kode Kelompok
    $data['KdKelompok'] = $this->session->userdata('laba_kelompok');
    //judul halaman 
    $judul              = 'Pencarian Laba';
    $data['judul']      = $judul;
    // Title HTML
    $this->layout->set_title($judul);
    //meta description jika perlu
    $this->layout->set_desc('Made with love by Raksa Abadi Informatika');

    // Memanggil JS
    $this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
    $this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
    //Memanggil CSS
    $this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
    $this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

    //breadcrumb/untuk navigasi
    $this->breadcrumb->clear();
    $this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
    $this->breadcrumb->add_crumb('Laporan', site_url('laporan_laba#'));
    $this->breadcrumb->add_crumb('Laporan Laba', site_url('laporan_laba'));
    $this->breadcrumb->add_crumb($judul);

		//menggunakan theme back
    $this->layout->theme('apotek','laporan_laba/index', $data);
  }

  function excel(){
  	$this->rule->type('R');
		//load our new PHPExcel library
  	$this->load->library('excel');
		//activate worksheet number 1
  	$this->excel->setActiveSheetIndex(0);
		//name the worksheet
  	$this->excel->getActiveSheet()->setTitle('Laporan Laba');
		//set cell A1 content with some text

    $tgl_awal     = $this->session->userdata('laba_tgl_awal');
    $tgl_akhir    = $this->session->userdata('laba_tgl_akhir');
    $KdKelompok   = $this->session->userdata('laba_kelompok');


    if($tgl_awal != '' && $tgl_akhir != '' && $KdKelompok != ''){
      $kelompok   = $this->m_laporan_laba->get_kelompok_by($KdKelompok);
      $this->excel->getActiveSheet()->setCellValue('A1', 'Laporan Laba Berdasarkan Kelompok '.$kelompok['NmKelompok'].' Pada tanggal '.id_date($tgl_awal).' s.d. '.id_date($tgl_awal).'');
    }else if($KdKelompok != ''){
      $kelompok   = $this->m_laporan_laba->get_kelompok_by($KdKelompok);
      $this->excel->getActiveSheet()->setCellValue('A1', 'Laporan Laba Berdasarkan Kelompok '.$kelompok['NmKelompok'].'');
    }else{
  	  $this->excel->getActiveSheet()->setCellValue('A1', 'Laporan Laba');
    }

  	$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
  	$this->excel->getActiveSheet()->setCellValue('B2', 'Kode Obat');
  	$this->excel->getActiveSheet()->setCellValue('C2', 'Nama Obat');
  	$this->excel->getActiveSheet()->setCellValue('D2', 'Kelompok Obat');
    $this->excel->getActiveSheet()->setCellValue('E2', 'Laba');
		//change the font size
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		//make the font become bold
  	$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
  	$this->excel->getActiveSheet()->mergeCells('A1:E1');
		//set aligment to center for that merged cell (A1 to D1)
  	$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		//set style
  	$TitleStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '00994C') // hijau tua
            ),
  		'font'  => array(
  			'bold'  => false,
  			'color' => array('rgb' => 'FFFFFF'),
  			'size'  => 15,
  			'name'  => 'Verdana'
  			)
  		);

  	$HeaderStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '32CD32') // hijau
            )
  		);

  	$ContentStyle = array(
  		'borders' => array(
  			'allborders' => array(
  				'style' => PHPExcel_Style_Border::BORDER_THIN
  				)
  			),
  		'fill' => array(
  			'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFFF00') // kuning
            )
  		);

  	$this->excel->getActiveSheet()->getStyle('A1:E1')->applyFromArray($TitleStyle);
  	$this->excel->getActiveSheet()->getStyle('A2:E2')->applyFromArray($HeaderStyle);

    if($tgl_awal != '' && $tgl_akhir != '' && $KdKelompok != ''){
      $list_data    = $this->m_laporan_laba->get_export_by_tanggal($tgl_awal,$tgl_akhir,$KdKelompok);
    }else if($KdKelompok != ''){
      $list_data    = $this->m_laporan_laba->get_export_by_kelompok($KdKelompok);
    }else{
      $list_data    = $this->m_laporan_laba->get_export_all();
    }

  	$cols = 3;
  	$num = 1;
  	foreach($list_data as $row){
      $laba = ($row['stok_akhir'] + $row['jual']) - ($row['stok_awal'] + $row['beli']);
  		$col = $cols++;
  		$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
  		$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['kdobat']);
  		$this->excel->getActiveSheet()->setCellValue('C' . $col, $row['nmobat']);
      $this->excel->getActiveSheet()->setCellValue('D' . $col, $row['nmkelompok']);
  		$this->excel->getActiveSheet()->setCellValue('E' . $col, $laba);
  	}

		//warna
  	$cols2 = 3;
  	foreach($list_data as $row){
  		$col2 = $cols2++;
  		$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('C' . $col2)->applyFromArray($ContentStyle);
  		$this->excel->getActiveSheet()->getStyle('D' . $col2)->applyFromArray($ContentStyle);
      $this->excel->getActiveSheet()->getStyle('E' . $col2)->applyFromArray($ContentStyle);
  	}

  	$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
  	$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
    $this->excel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);

		$filename='laporan laba.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache

		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

}
